package cn.wolfcode.crm.util;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;

@ControllerAdvice //controller增强切面
public class ExceptionsHandler {
    @ExceptionHandler(UnauthorizedException.class)  //指定处理异常类型
    public void exceptionHandler(HandlerMethod method, HttpServletResponse resp) throws  Exception{
        ResponseBody body = method.getMethodAnnotation(ResponseBody.class);
        if (body != null) {
            //ajax请求方式
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write("{\"success\":false,\"msg\":\"没有此操作权限\",\"total\":0,\"rows\":[]}");
        } else {
            //普通页面,跳到nopermission.jsp页面
            resp.setContentType("text/html;charset=utf-8");
            resp.sendRedirect("/nopermission.jsp");
        }

    }
}
